import { getUserInformResponse, getUserGroupsListResponse, getMessagesListResponse, NormalResponse } from "./vo";
import ajax from "../../../modules/ajax";
import config from "../../../config.json";

export async function getUserInformation(): Promise<getUserInformResponse> {
    let res = await ajax.ajax(config.requestUrl + "/gapi/user/get", {
        method: "GET"
    });
    if(res.ok) {
        return res.json();
    } else {
        return Promise.reject("错误状态码:" + res.statusCode);
    }
}
export async function getUserGroupsList(): Promise<getUserGroupsListResponse> {
    let res = await ajax.ajax(config.requestUrl + "/gapi/group/getList", {
        method: "GET"
    });
    if(res.ok) {
        return res.json();
    } else {
        return Promise.reject("错误状态码:" + res.statusCode);
    }
}

export async function getMessagesList(): Promise<getMessagesListResponse> {
    let res = await ajax.ajax(config.requestUrl + "/gapi/message/get", {
        method: "GET"
    });
    if(res.ok) {
        return res.json();
    } else {
        return Promise.reject("错误状态码:" + res.statusCode);
    }
}

export async function handleMessage(body:any): Promise<NormalResponse> {
    let res = await ajax.ajax(config.requestUrl + "/gapi/message/handle", {
        method: "POST",
        body: JSON.stringify(body),
        headers: {
            "Content-Type" : "application/json",
        },
    });
    if(res.ok) {
        return res.json();
    } else {
        return Promise.reject("错误状态码:" + res.statusCode);
    }
}